home *** CD-ROM | disk | FTP | other *** search
/ Disc to the Future 2 / Disc to the Future Part II Programmer's Reference (Wayzata Technology)(6013)(1992).bin / MAC / MPW_C / SPELL__ / INIT.C < prev    next >
Text File  |  1990-07-03  |  3KB  |  107 lines

  1. /*
  2.  
  3.     File:    init.c
  4.     Authors: Richard Hooker, Graham Toal
  5.     Purpose: Loading of dictionary for spelling checker.
  6.     Functions exported:  dawg_init, pack_init
  7.     Internal functions:  spell_init
  8.  
  9. Description:
  10.  
  11. The address of a dictionary (either a PACKed dict or a DAWG) is set up by
  12. this procedure.  This gives us the option of connecting the dict in read-only
  13. (or copy-on-write) virtual memory.  On non-VM machines, we simply allocate a
  14. large buffer into which the relevant part of the dictionary is loaded.
  15.  
  16. The magic number is used to check the dict type, *and* the machine byte-sex.
  17. If the sex is reversed, even a VM system has to load the data into writable
  18. memory (so that it can reverse it).
  19.  
  20. */
  21.  
  22. /*######################### INTERNAL FUNCTIONS #########################*/
  23.  
  24.  
  25. static int
  26. #ifdef PROTOTYPES
  27. spell_init(char *filename, NODE PCCRAP **dictp,
  28.   char *DEFAULT_DICT, long magic_number, INDEX *nedges)
  29. #else
  30. spell_init(filename, dictp, DEFAULT_DICT, magic_number, nedges)
  31. char *filename;
  32. NODE PCCRAP **dictp;
  33. char *DEFAULT_DICT;
  34. long magic_number;
  35. INDEX *nedges;
  36. #endif
  37. #define dict (*dictp)
  38. {
  39. FILE *fp; INDEX count;
  40.  
  41.   /*  init_dict("") gets default */
  42.   if (*filename == '\0') filename = DEFAULT_DICT;
  43.  
  44.   /* Open the file and find out the size of the dict -- which
  45.      is stored in the first word.  Later I'll change the dict format
  46.      to have a header, and the header will have to be skipped by
  47.      this module. */
  48.  
  49.   if ((fp = fopen(filename, "rb")) == NULL) {
  50.     fprintf (stderr, "Can\'t open file \"%s\"\n", filename);
  51.     return(FALSE);
  52.   }
  53.   *nedges = getword(fp);
  54. #ifdef DEBUG
  55. fprintf(stderr, "dawg contains %8lx edges\n", *nedges);
  56. #endif
  57.   /* Allocate precisely enough memory for all edges + 0 at root node. */
  58.   if ((dict = MALLOC((SIZE_T)((*nedges)+1), sizeof(NODE PCCRAP *))) == 0) {
  59.     fprintf (stderr, "Can\'t allocate space for dictionary\n");
  60.     return(FALSE);
  61.   }
  62.  
  63.   dict[0] = 0; /* Root node set to 0; terminal nodes point to 0. */
  64.  
  65.   /* Load in the dictionary.  Routine 'getwords' should be efficient */
  66.   count = getwords(&dict[1], (long)(4*(*nedges)), fp);
  67.   if (count != 4*(*nedges)) {
  68.     fprintf(stderr,
  69.       "Failed to read dictionary %s - wanted %ld bytes, got %ld\n",
  70.       filename, 4*(*nedges), count);
  71.     return(FALSE);
  72.   }
  73.   fclose(fp);
  74.  
  75.   return(TRUE);
  76. #undef dict
  77. }
  78.  
  79. /*####################### EXPORTED FUNCTIONS #########################*/
  80.  
  81. int
  82. #ifdef PROTOTYPES
  83. dawg_init(char *filename, NODE PCCRAP **dawgp, INDEX *nedges)
  84. #else
  85. dawg_init(filename, dawgp, nedges)
  86. char *filename;
  87. NODE PCCRAP **dawgp;
  88. INDEX *nedges;
  89. #endif
  90. {
  91.   return(spell_init(filename, dawgp, DEFAULT_DAWG, DAWG_MAGIC, nedges));
  92. }
  93.  
  94. int
  95. #ifdef PROTOTYPES
  96. pack_init(char *filename, NODE PCCRAP **packp, INDEX *nedges)
  97. #else
  98. pack_init(filename, packp, nedges)
  99. char *filename;
  100. NODE PCCRAP **packp;
  101. INDEX *nedges;
  102. #endif
  103. {
  104.   return(spell_init(filename, packp, DEFAULT_PACK, PACK_MAGIC, nedges));
  105. }
  106.  
  107.